package com.huangc.cn.rainbow.properties;

import lombok.Data;
import org.assertj.core.util.Lists;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author huangc
 */
@Component
@ConfigurationProperties(prefix = "china")
@Data
public class CityConfig {

    private List<String> province;

    private List<String> city;

    private String[] provinces;

    private String[][] cityList;

    public void parse() {
        if (provinces != null) {
            return;
        }

        cityList = new String[province.size()][];

        int i = 0;
        for (String str: city) {
            List<String> cityList = new ArrayList<>();
            if (StringUtils.isEmpty(str)) {
                cityList.add("--");
            } else {
                String[] cityArray = str.split(" ");
                cityList = Lists.list(cityArray);
            }
            this.cityList[i] = cityList.toArray(new String[]{});
            i++;
        }
        this.provinces = province.toArray(new String[] {});
    }


    public String[] getProvinces() {
        parse();
        return this.provinces;
    }

    public String[][] getCityList() {
        parse();
        return this.cityList;
    }

    public String[] getCity(String province) {
        int index = province.indexOf(province);
        return cityList[index];
    }
}
